package com.cn.core.utils;

import java.util.Locale;

public final class PropertyNameUtils {

    private PropertyNameUtils() {
        // Prevent Instantiation of Static Class
    }

    /**
     * 将方法名转化为属性， 如果方法名前缀含有 is、get、set都会替换为空
     *
     * @param methodName 方法名
     * @return {@link String }
     */
    public static String methodToProperty(String methodName) {
        if (methodName.startsWith("is")) {
            methodName = methodName.substring(2);
        } else if (methodName.startsWith("get") || methodName.startsWith("set")) {
            methodName = methodName.substring(3);
        } else {
            throw new RuntimeException("Error parsing property name '" + methodName + "'.  Didn't start with 'is', 'get' or 'set'.");
        }

        if (methodName.length() == 1 || (methodName.length() > 1 && !Character.isUpperCase(methodName.charAt(1)))) {
            methodName = methodName.substring(0, 1).toLowerCase(Locale.ENGLISH) + methodName.substring(1);
        }

        return methodName;
    }

    /**
     * 是否是属性
     *
     * @param name 名字
     * @return boolean
     */
    public static boolean isProperty(String name) {
        return isGetter(name) || isSetter(name);
    }

    /**
     * 是 get方法
     *
     * @param name 名字
     * @return boolean
     */
    public static boolean isGetter(String name) {
        return (name.startsWith("get") && name.length() > 3) || (name.startsWith("is") && name.length() > 2);
    }

    /**
     * 是 set方法
     *
     * @param name 名字
     * @return boolean
     */
    public static boolean isSetter(String name) {
        return name.startsWith("set") && name.length() > 3;
    }

}